/**
 * 
 */
package wpmp.security.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import wcommons.lang.UrlUtils;
import wcommons.lang.properties.PropertiesUtils;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 3:06:20 PM Apr 30, 2014
 */
public final class SecurityConfig {

	private static final String AUTH_SERVER = "authServer";
	private static final String LOGIN_URL = "loginUrl";
	private static final String BUTTON_CONFIG_CACHE_TIMEOUT = "buttonConfigCacheTimeout";

	// 权限认证服务的地址
	private static String authServer;
	// 登录的地址，未登录时重定向用到
	private static String loginUrl;
	// 按钮配置的客户端缓存失效时间
	private static long buttonConfigCacheTimeout = 120;

	public void initByProperties() {
		initByProperties("/wpmp-security.properties");
	}

	public void initByProperties(String propertiesFile) {
		InputStream input = null;

		try {
			final File file = new File(propertiesFile);
			if (file.exists()) {
				input = new FileInputStream(file);
			}
			else {
				input = SecurityConfig.class.getResourceAsStream(propertiesFile);
				if (input == null) {
					input = SecurityConfig.class.getClassLoader().getResourceAsStream(propertiesFile);
				}
			}
			if (input == null) {
				throw new InitException("can not find " + propertiesFile);
			}

			final Properties properties = new Properties();
			properties.load(input);
			initByProperties(properties);
			assertProperties(properties);
		}
		catch (Exception e) {
			throw new InitException("Error to load from " + propertiesFile, e);
		}
		finally {
			IOUtils.closeQuietly(input);
		}
	}

	public void initByProperties(Properties properties) {
		setAuthServer(properties.getProperty(AUTH_SERVER).trim());
		setLoginUrl(properties.getProperty(LOGIN_URL).trim());
		setButtonConfigCacheTimeout(PropertiesUtils.getPropertyForLong(properties, BUTTON_CONFIG_CACHE_TIMEOUT, -1L));
	}

	void assertProperties(Properties properties) {
		if (StringUtils.isBlank(properties.getProperty(AUTH_SERVER))) {
			throw new InitException(AUTH_SERVER + "must not be blank");
		}
		if (StringUtils.isBlank(properties.getProperty(LOGIN_URL))) {
			throw new InitException(LOGIN_URL + "must not be blank");
		}
	}

	public static String getAuthServer() {
		return authServer;
	}

	public static String getAuthUrl(String uri) {
		return UrlUtils.spliceUrls(getAuthServer(), uri);
	}

	public void setAuthServer(String authServer) {
		if (StringUtils.isBlank(authServer)) {
			throw new InitException("Property[" + AUTH_SERVER + "] is required");
		}

		SecurityConfig.authServer = authServer;
	}

	public static String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		if (StringUtils.isBlank(loginUrl)) {
			throw new InitException("Property[" + LOGIN_URL + "] is required");
		}

		SecurityConfig.loginUrl = loginUrl;
	}

	public static long getButtonConfigCacheTimeout() {
		return buttonConfigCacheTimeout;
	}

	public void setButtonConfigCacheTimeout(long buttonConfigCacheTimeout) {
		if (buttonConfigCacheTimeout > 0) {
			SecurityConfig.buttonConfigCacheTimeout = buttonConfigCacheTimeout;
		}
	}

}
